#include "splitwidget.h"
#include <QPushButton>
#include <QLineEdit>
#include <QCheckBox>
#include <QVBoxLayout>
#include <QGridLayout>
#include <QHBoxLayout>
#include <QScrollBar>
#include <QRadioButton>
#include <QLabel>
#include <QProgressBar>
#include <QFileDialog>
#include <QApplication>

CSplitWidget::CSplitWidget(QWidget *parent)
    : QWidget(parent)
{
    CreateUi();
    SetupUi();
    UpdateUiText();
}

CSplitWidget::~CSplitWidget()
{

}

void CSplitWidget::CreateUi()
{
    m_LedtData = new QLineEdit();
    m_pLbData = new QLabel();

    m_pLbCbType = new QLabel();
    m_pCbType1 = new QRadioButton();
    m_pCbType2 = new QRadioButton();

    m_pBtnSplit = new QPushButton();
    m_pBtnChooseFile = new QPushButton();

    m_pLedtInPath = new QLineEdit();
    m_Pgb = new QProgressBar();

    QGridLayout* pLayOut = new QGridLayout();

    pLayOut->addWidget(m_pLbCbType, 0, 0);
    pLayOut->addWidget(m_pCbType1, 0, 1);
    pLayOut->addWidget(m_pCbType2, 0, 2);
    pLayOut->addWidget(m_LedtData, 0, 3);
    pLayOut->addWidget(m_pLbData, 0, 3);

    pLayOut->addWidget(m_pLedtInPath, 1,0,1,3);
    pLayOut->addWidget(m_pBtnChooseFile, 1,3);

    pLayOut->addWidget(m_Pgb, 2, 0, 1, 3);
    pLayOut->addWidget(m_pBtnSplit, 2,3);
    setLayout(pLayOut);
}

void CSplitWidget::UpdateUiText()
{
    m_pLbCbType->setText(tr("Split By:"));
    m_pBtnChooseFile->setText(tr("&Choose a file..."));
   // m_pBtnChooseFile->setFixedSize(80, 24);

    m_pBtnSplit->setText(tr("&Start Split"));
    m_pCbType1->setText(tr("Count"));
    m_pCbType2->setText(tr("Size"));
}

void CSplitWidget::SetupUi()
{
    //TODO:
    connect(m_pBtnChooseFile, SIGNAL(clicked()), this, SLOT(slotChooseFile()));
//  TODO:: languageChange;
//    connect(qApp, SIGNAL())
}

#define KB_SIZE (1024U)
#define MB_SIZE (KB_SIZE*1024)
#define GB_SIZE (1024U*MB_SIZE)
#define TB_SIZE (1024*GB_SIZE)

QString formatFileSize(quint64 size)
{
    QString fmt;
    if (size >= TB_SIZE)
    {
        fmt = QString("%1 TB").arg(1.0*size/GB_SIZE);
    }
    else if (size >= GB_SIZE)
    {
        fmt = QString("%1 GB").arg(1.0*size/GB_SIZE);
    }
    else if (size >= MB_SIZE)
    {
        fmt = QString("%1 MB").arg(1.0*size/MB_SIZE);
    }
    else if(size >= KB_SIZE)
    {
        fmt = QString("%1 KB").arg(1.0*size/KB_SIZE);
    }
    else
    {
        fmt = QString("%1 B").arg(size);
    }
    return fmt;
}

void CSplitWidget::slotChooseFile()
{
    QString fileName = QFileDialog::getOpenFileName(this, tr("Open File"),
                                                     ".",
                                                     tr("files (*.*)"));
    if (!fileName.isEmpty())
    {
        m_pLedtInPath->setText(fileName);
    }
    QFileInfo fileinfo(fileName);
    QString size = formatFileSize(fileinfo.size());
    m_pLbData->setText(size);
}
